package com.zuowei.ycy.user.ui.mine;

import android.net.Uri;
import android.view.View;
import android.widget.TextView;

import com.and.yzy.frame.util.RetrofitUtils;
import com.facebook.drawee.view.SimpleDraweeView;
import com.zuowei.ycy.R;
import com.zuowei.ycy.user.config.UserManger;
import com.zuowei.ycy.user.domain.UserInfo;
import com.zuowei.ycy.user.http.Member;
import com.zuowei.ycy.ui.BaseFgt;
import com.zuowei.ycy.user.ui.login.LoginAty;
import com.zuowei.ycy.user.ui.mine.balance.MineBalanceAty;
import com.zuowei.ycy.user.util.AppJsonUtil;

import butterknife.Bind;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/4/25.
 */
public class MineFgt extends BaseFgt {
    @Bind(R.id.iv_head)
    SimpleDraweeView ivHead;
    @Bind(R.id.tv_name)
    TextView tvName;

    @Override
    public int getLayoutId() {
        return R.layout.mine_fgt;
    }

    @Override
    public void initData() {

    }

    @Override
    public boolean setIsInitRequestData() {
        return true;
    }

    @Override
    public void onUserVisible() {
        super.onUserVisible();
//        doHttp(RetrofitUtils.createApi(Member.class).memberCenter());
        if (!UserManger.isLogin()) {
            return;
        }
        doHttp(RetrofitUtils.createApi(Member.class).memberBaseData(UserManger.getUserInfo().getM_id()), 0);
    }

    @Override
    public void requestData() {
        if (!UserManger.isLogin()) {
            return;
        }
        showLoadingContentDialog();
        doHttp(RetrofitUtils.createApi(Member.class).memberBaseData(UserManger.getUserInfo().getM_id()), 0);
    }

    @OnClick({R.id.tv_balance, R.id.tv_mine_collect, R.id.tv_address, R.id.imgv_setting, R.id.iv_head})
    @Override
    public void btnClick(View view) {
        super.btnClick(view);
        switch (view.getId()) {
            case R.id.tv_balance:
                if (!UserManger.isLogin()){
                    startActivity(LoginAty.class,null);
                    return;
                }
                startActivity(MineBalanceAty.class, null);
                break;
            case R.id.tv_mine_collect:
                startActivity(MineCollectAty.class, null);
                break;
            case R.id.tv_address:
                if (!UserManger.isLogin()){
                    startActivity(LoginAty.class,null);
                    return;
                }

                startActivity(AddressListAty.class, null);
                break;
            case R.id.imgv_setting:
                startActivity(MineSystemSettingAty.class, null);
                break;
            case R.id.iv_head:
                startActivity(MineDataAty.class, null);
                break;
        }
    }

    @Override
    public void onSuccess(String result, Call<ResponseBody> call, Response<ResponseBody> response, int what) {
        super.onSuccess(result, call, response, what);
        if (what==0){
            UserInfo info = AppJsonUtil.getObject(result,UserInfo.class);
            UserManger.setUserInfo(info);
            ivHead.setImageURI(Uri.parse(info.getHead_pic()));
            tvName.setText(info.getNickname());
        }


    }
}
